package pl.edu.agh.portal.application.portlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.utils.PortletsUtils;
import pl.edu.agh.thesis.webservices.employee.client.EmployeeUtils;
import pl.edu.agh.thesis.webservices.project.client.ProjectManagerStub;
import pl.edu.agh.thesis.webservices.project.client.ProjectUtils;
import pl.edu.agh.thesis.webservices.project.client.ProjectManagerStub.ReturnState;

public class ProjectCreatePortlet extends ThesisGenericPortlet {

	private static final String NORMAL_VIEW = "/project_create_portlet.jsp";
	
	private PortletRequestDispatcher normalView;
	
	private Map<String, String> errorList = new HashMap<String, String>();
	
	private ProjectManagerStub.Project temporaryProject;	
		
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
		
		if (errorList.size() == 0) {
			temporaryProject = new ProjectManagerStub.Project();
		}
			
		request.setAttribute(PortletsUtils.ATTRIBUTE_ERROR_MESSAGES, errorList);
		request.setAttribute(PortletsUtils.ATTRIBUTE_PROJECT_FOR_CREATION, temporaryProject);
		normalView.include(request, response);
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {				
		String value = request.getParameter(PortletsUtils.PROJECT_CREATE_KEY);	
		String address = "/portal/portal/user_main.psml"; 
		if (value.equals(PortletsUtils.BUTTON_CANCEL)) {
			errorList = new HashMap<String, String>();			
			response.sendRedirect(address);
		} else if (value.equals(PortletsUtils.BUTTON_CREATE)) {
			validate(request);
			if (errorList.size() == 0) {								
				ReturnState r = ProjectUtils.create(PortletsUtils.temp_session, temporaryProject.getProjectName(), temporaryProject.getDescription());				
				String[] participants = request.getParameterValues(PortletsUtils.PARAMETER_PROJECT_USERS);
				
				if (participants != null) {
					for (int i=0; i<participants.length; i++) {						
						EmployeeUtils.assign(PortletsUtils.temp_session, Integer.parseInt(participants[i]), r.getState(), PortletsUtils.ROLE_USER);
					}					
				}
				
				response.sendRedirect(address);	
			}			
		} else {
			response.setPortletMode(PortletMode.VIEW);
		}		
		
	}	
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);
	}
	
	public void destroy() {
		normalView = null;
		super.destroy();
	}

	private void validate(ActionRequest request) {
		errorList = new HashMap<String, String>();
		
		String parameterProjectName = request.getParameter(PortletsUtils.PARAMETER_PROJECT_NAME);
		String parameterProjectDescription = request.getParameter(PortletsUtils.PARAMETER_PROJECT_DESCRIPTION);
				
		if (parameterProjectName != null && parameterProjectName.trim().equals("")) {
			errorList.put(PortletsUtils.ERROR_PARAMETER_PROJECT_NAME, PortletsUtils.ERROR_PARAMETER_PROJECT_NAME);
		} else {
			temporaryProject.setProjectName(parameterProjectName);
		}
		if (parameterProjectDescription != null && parameterProjectDescription.trim().equals("")) {			
			errorList.put(PortletsUtils.ERROR_PARAMETER_PROJECT_DESCRIPTION, PortletsUtils.ERROR_PARAMETER_PROJECT_DESCRIPTION);
		} else {
			temporaryProject.setDescription(parameterProjectDescription);			
		}	
	}
	
}
